package com.tools.s7.recycler.utils;

import android.support.v7.widget.RecyclerView;

import com.tools.s7.recycler.BaseRecyclerLoadAdapter;
import com.tools.s7.recycler.listener.Recycler;

import java.util.List;


public class LoadHelper<T> {

    private boolean hasInitAdapter = false;

    private int page = 1;

    private boolean isPage = false;
    private boolean isAadditional = true;
    private BaseRecyclerLoadAdapter<T> mAdapter;
    private RecyclerView mRecycler;
    private Recycler.OnCheckedChangeListener<T> mCheckedChange;
    private Recycler.OnClickListener<T> mClick;
    private Recycler.OnItemClickListener<T> mItemClick;
    private Recycler.OnItemLongClickListener<T> mItemLongClick;
    private LoadDataListener mLoadData;


    public LoadHelper setAadditional(boolean aadditional) {
        isAadditional = aadditional;
        return this;
    }


    protected LoadHelper setAdapter(BaseRecyclerLoadAdapter<T> mAdapter) {
        this.mAdapter = mAdapter;
        return this;
    }

    protected LoadHelper setRecycler(RecyclerView mRecycler) {
        this.mRecycler = mRecycler;
        return this;
    }

    public LoadHelper setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
        this.mCheckedChange = mCheckedChange;
        return this;

    }

    protected LoadHelper setClick(Recycler.OnClickListener<T> mClick) {
        this.mClick = mClick;
        return this;
    }

    protected LoadHelper setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
        this.mItemClick = mItemClick;
        return this;
    }

    protected LoadHelper setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
        this.mItemLongClick = mItemLongClick;
        return this;
    }

    protected LoadHelper setLoadData(LoadDataListener mLoadData) {
        this.mLoadData = mLoadData;
        return this;
    }

    public void initAdapter(){

        if (mCheckedChange != null)
            mAdapter.setCheckedChange(mCheckedChange);

        if (mClick != null)
            mAdapter.setClick(mClick);

        if (mItemClick != null)
            mAdapter.setItemClick(mItemClick);

        if (mItemLongClick != null)
            mAdapter.setItemLongClick(mItemLongClick);

        mAdapter.setAadditional(isAadditional);
        if (mLoadData != null) {
            mAdapter.setLoadingListener(new Recycler.OnLoadingListener() {
                @Override
                public void onReload() {
                    page = 1;
                    mLoadData.onLoadData(page);
                }

                @Override
                public void onLoading() {
                    mLoadData.onLoadData(page);
                }
            });
        }

        mRecycler.setAdapter(mAdapter);
        hasInitAdapter = true;
    }

    public void loadFirstPage() {
        page = 1;
        if(!hasInitAdapter){
            initAdapter();
        }
        if (mLoadData != null) {
            mLoadData.onLoadData(page);
        }
    }

    public void notifyAdapter(List<T> datas) {
        if (page == 1) {
            mAdapter.onRefresh(datas);
        } else {
            mAdapter.addDatas(datas);
        }
        if (!isPage) {
            page++;
        }
    }

    public void notifyAdapter() {
        mAdapter.notifyDataSetChanged();
    }

    public void notifyError() {
        mAdapter.setLoadError(true);
    }

    public void notifyRefresh(List<T> datas) {
        mAdapter.onRefresh(datas);
    }

    public void notifyReplace(int position, T data) {
        mAdapter.onReplace(position, data);
    }

    public void notifyRemove(int position) {
        mAdapter.onRemove(position);
    }

    public List<T> getData() {
        return mAdapter.getDatas();
    }


    public void setPage(boolean page) {
        isPage = page;
    }

    public static class Builder<T> {
        private BaseRecyclerLoadAdapter<T> mAdapter;
        private RecyclerView mRecycler;
        private Recycler.OnCheckedChangeListener<T> mCheckedChange;
        private Recycler.OnClickListener<T> mClick;
        private Recycler.OnItemClickListener<T> mItemClick;
        private Recycler.OnItemLongClickListener<T> mItemLongClick;
        private LoadDataListener mLoadData;
        private boolean isAadditional = true;

        public Builder setAdapter(BaseRecyclerLoadAdapter<T> mAdapter) {
            this.mAdapter = mAdapter;
            return this;
        }

        public Builder setAadditional(boolean isAadditional) {
            this.isAadditional = isAadditional;
            return this;
        }


        public Builder setAdapter(BaseRecyclerLoadAdapter<T> mAdapter, boolean isLoad) {
            this.mAdapter = mAdapter;
            this.mAdapter.setLoad(isLoad);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, RecyclerView.LayoutManager layoutManager) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, RecyclerView.LayoutManager layoutManager, RecyclerView.ItemDecoration itemDecoration) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            this.mRecycler.addItemDecoration(itemDecoration);
            return this;
        }

        public Builder setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
            this.mCheckedChange = mCheckedChange;
            return this;
        }

        public Builder setClick(Recycler.OnClickListener<T> mClick) {
            this.mClick = mClick;
            return this;
        }

        public Builder setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
            this.mItemClick = mItemClick;
            return this;
        }

        public Builder setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
            this.mItemLongClick = mItemLongClick;
            return this;
        }

        public Builder setLoadData(LoadDataListener mLoadData) {
            this.mLoadData = mLoadData;
            return this;
        }

        public <T> LoadHelper<T> build() {
            LoadHelper mHelper = new LoadHelper<T>()
                    .setAadditional(isAadditional)
                    .setAdapter(mAdapter)
                    .setRecycler(mRecycler)
                    .setCheckedChange(mCheckedChange)
                    .setClick(mClick)
                    .setItemClick(mItemClick)
                    .setItemLongClick(mItemLongClick)
                    .setLoadData(mLoadData);
            mHelper.initAdapter();
            return mHelper;
        }
    }

    public interface LoadDataListener {
        void onLoadData(int page);
    }
}
